package GDoc::Job::Fetch::US::Exec::HS::FA::TFRS;

use Moose;
use WWW::Mechanize;

use GDoc::Job;

extends 'GDoc::Job::Fetch';

sub setup {
    my $self = shift;
    $self->_baseurl( 'http://www.usfa.dhs.gov/statistics/reports/' );
    $self->_basedir( 'http://www.usfa.dhs.gov' );
}

sub run { 
    my $self = shift;

    if ( $self->data->{link} ) { 
        $self->_fetch_pdf;
    } else { 
        $self->logger->info( "fetching base " . $self->_baseurl );
        my $mech = $self->_mech;
        $mech->get( $self->_baseurl );

        my @vol_links = $mech->find_all_links( url_regex => qr{tfrsv\d+\.shtm}i );

        foreach my $link( @vol_links ) { 
            $self->logger->info( "Found volume link " . $link->url );
            $mech->get( $link );  
            my @pdf_links = $mech->find_all_links( url_regex => qr{v\d+i\d+(-\d+)?\.pdf$}i );

            foreach my $pdf_link( @pdf_links ) { 
                my $desc = $pdf_link->text;
                $desc =~ s/\(PDF,.*?\)//;
                $desc =~ s/PDF,\s[\d\.]{2,5}\s[KM]b//;

                my $job = GDoc::Job->new( class => 'Fetch::US::Exec::HS::FA::TFRS',
                                          queue => $self->fetch_queue,
                                          data  => { link    => $self->_basedir . $pdf_link->url,
                                                     desc    => $desc
                                                   }
                                        );

                $job->submit;
            }
        }
    }
}

sub _fetch_pdf { 
    my $self = shift;
    my %data = %{ $self->data };

    $data{link} =~ m{v(?<vol>\d+)i(?<iss>\d+)(-\d+)?\.pdf}i;
    my $vol = $+{vol};
    my $iss = $+{iss};

    unless( $vol and $iss ) { 
        $self->logger->error( "Unable to get volume and issue number from $data{link}" );
        return;
    }

    $self->logger->info( "Fetching $data{link}" );
    my $mech = $self->_mech;
    $mech->get( $data{link} );

    my $key = sprintf( 'us/exec/hs/fa/FA-TFR-%02d-%02d.pdf', $vol, $iss ); 
    $self->logger->info( "Storing $data{link} --> $key" );

    $self->store_pdf( key        => $key,
                      data       => $mech->response->content,
                      filename   => sprintf( 'FA-TFR-%02d-%02d.pdf', $vol, $iss ),
                      meta       => { description   => $data{desc},
                                      identifier    => sprintf( 'FA-TFR-%02d-%02d', $vol, $iss )
                                    }
                    );
                      
}




1;
